//import checkoutRulesJson from './val.json'
// import checkoutRulesJson from './valTjcb.json'
import { Message } from "element-ui";
/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
export function validURL(url) {
  const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return reg.test(url)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str) {
  const reg = /^[a-z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str) {
  const reg = /^[A-Z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str) {
  const reg = /^[A-Za-z]+$/
  return reg.test(str)
}

/**
 * @param {string} email
 * @returns {Boolean}
 */
export function validEmail(email) {
  const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  return reg.test(email)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(str) {
  if (typeof str === 'string' || str instanceof String) {
    return true
  }
  return false
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg) {
  if (typeof Array.isArray === 'undefined') {
    return Object.prototype.toString.call(arg) === '[object Array]'
  }
  return Array.isArray(arg)
}

/**
 * 获取表单校验规则 (流程相关)  
 */
export function getCheckFromRules(params, nodeid, vm) { 
  //读取在线配置规则
  const checkoutRulesJson = vm.$store.state.common.pagesRules.rules 
  let currentRoute = vm.$store.state.common.mainTabsActiveName 
  currentRoute = currentRoute.replace(/-[0-9]+/, '') 
  let findRules = checkoutRulesJson.find(item => item.name == currentRoute)
  if (!findRules) {
    return {}
  }
  let arrs = findRules.formRules.filter(item => item.nodeid.includes(nodeid)) 
  const rule = {}
  //动态规则
  if (nodeid) {
    Object.keys(params).forEach(item => { 
      let obj = arrs.find(el => el.name == item)
      if (obj) {
        rule[`${obj.name}`] = obj.itemRules
      }
    }); 
  } 
  // 公共规则添加
  findRules.publicRules.forEach(item => {
    rule[`${item.name}`] = item.itemRules
  }) 
  console.log(rule,'规则');
  return rule
}
export function getTableRules(params, codes, nodeid, vm) {
  let currentRoute = vm.$store.state.common.mainTabsActiveName
  ////获取在线规则
  const checkoutRulesJson = vm.$store.state.common.pagesRules.rules
  currentRoute = currentRoute.replace(/-[0-9]+/, '')
  let findRules = checkoutRulesJson.find(item => item.name == currentRoute) 
  if (!findRules) {
    return true
  }
  let arrs = findRules.tableRules.filter(item => codes.includes(item.name) && item.nodeid.includes(nodeid))
  //无规则
  if (!arrs.length) {
    return true
  } 
  try {
    arrs.forEach((item, index) => {
      let data = params[`${item.name}`] 
      if (item.length > 0 && data.length >= item.length) {
        if (item.tableInputRules.length) {
          let flag = handleRules(data, item.tableInputRules)
          //表格输入框校验失败
          if (!flag) {
            throw Error(`error`)
          }
        }
      } else {
        Message.error(item.message)
        throw Error(`error`)
      }
    })
  } catch (err) {
    return false
  }
  return true
}
/**
 *  表格行选项校验
 */
function handleRules(parames, rules) {
  for (const item of parames) {
    for (const elem of rules) {
      let data = Object.keys(item).find(key => elem.name == key)
      if (!item[`${data}`]) {
        Message.error(elem.message)
        return false
      }
    }
  }
  return true
} 


/**
 * 获取表单校验规则 (流程相关 tjcb 分社)  
 */
export function getDifferentEditionCheckFromRules(params, nodeid, vm) {
  const  publisherid = vm.$store.state.user.publisherid
  //读取在线配置规则
   const checkoutRulesJson = vm.$store.state.common.pagesRules.rules
  let currentRoute = vm.$store.state.common.mainTabsActiveName
  currentRoute = currentRoute.replace(/-[0-9]+/, '')
  
  let findRules = checkoutRulesJson.find(item => item.name == currentRoute)
  if (!findRules) {
    return {}
  } 
  const arrs1 = findRules.formRules.publicFlowNodeRules.filter(item => item.nodeid.includes(nodeid) )
  const data = findRules.formRules.itemEditionFlowNodeRules.find(item => item.publisherid == publisherid )
  let  arrs2 = ( data && data.itemRulesList ) || []
  arrs2 = arrs2.filter(item => item.nodeid.includes(nodeid))
  const rule = {}  
  //动态规则
  if (nodeid) {
    let ruleList = [...arrs1, ...arrs2 ]
    Object.keys(params).forEach(item => {
      let obj = ruleList.find(el => el.name == item) 
      if (obj) { 
        rule[`${obj.name}`] = obj.itemRules
      }
    });
  } 
  //// 公共规则添加
  let itemEditionRules = findRules.publicRules.itemEditionRules.find(item => item.publisherid == publisherid )
  //分社单独流程公共规则  
  if (itemEditionRules&&itemEditionRules.itemRulesList) {
    itemEditionRules.itemRulesList.forEach(el => {
      rule[`${el.name}`] = el.itemRules
    })
  } 
  // 所有分社流程公共规则
  findRules.publicRules.requiredRules.forEach(item => {
    rule[`${item.name}`] = item.itemRules
  })
  console.log(rule,'最终规则')
  return rule
}